package com.yugu.park.system.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageInfo;
import com.yugu.util.SearchPage;
import com.yugu.util.StringUtils;

import cn.hutool.core.lang.UUID;

import com.yugu.park.system.mapper.SysDictDataMapper;
import com.yugu.park.system.entity.SysDictDataEntity;
import com.yugu.park.system.service.SysDictDataService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictDataEntity> implements SysDictDataService {
      @Resource
	  private SysDictDataMapper mapper;
	@Override
	public PageInfo<SysDictDataEntity> page(SearchPage<SysDictDataEntity> search) {
		PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<SysDictDataEntity> list = mapper.page(search.getSearch());
        return PageInfo.of(list);
	}
	@Override
	public void addOrUpdate(SysDictDataEntity t) {
		SysDictDataEntity old = mapper.selectById(t.getId());
		if(old!=null) {
			String cssClass = t.getCssClass();
			if(StringUtils.isNotEmpty(cssClass)) {
				old.setCssClass(cssClass);
			}
			String dictCode = t.getDictCode();
			if(StringUtils.isNotEmpty(dictCode)) {
				old.setDictCode(dictCode);
			}
			String dictLabel = t.getDictLabel();
			if(StringUtils.isNotEmpty(dictLabel)) {
				old.setDictLabel(dictLabel);
			}
			Integer dictSort = t.getDictSort();
			if(dictSort!=null) {
				old.setDictSort(dictSort);
			}
			String dictType = t.getDictType();
			if(StringUtils.isNotEmpty(dictType)) {
				old.setDictType(dictType);
			}
			String dictValue = t.getDictValue();
			if(StringUtils.isNotEmpty(dictValue)) {
				old.setDictValue(dictValue);
			}
			String isDefault = t.getIsDefault();
			if(StringUtils.isNotEmpty(isDefault)) {
				old.setIsDefault(isDefault);
			}
			String listClass = t.getListClass();
			if(StringUtils.isNotEmpty(listClass)) {
				old.setListClass(listClass);
			}
			String remark = t.getRemark();
			if(StringUtils.isNotEmpty(remark)) {
				old.setRemark(remark);
			}
			String status = t.getStatus();
			if(StringUtils.isNotEmpty(status)) {
				old.setStatus(status);
			}
			mapper.updateById(old);
		}else{
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			t.setId(id);
			t.setDictCode(id);
			mapper.insert(t);
		}
	}
	@Override
	public List<SysDictDataEntity> getByTypeId(String typeId) {
		QueryWrapper<SysDictDataEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dict_type", typeId);
		queryWrapper.orderByAsc("dict_sort");
		return mapper.selectList(queryWrapper);
	}

}
